package com.itheima.reflectstructdemo;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectClass {

    public static void saveObject(Object obj){
        // 反射获取类对象
        Class c = obj.getClass();
        // 获取类对象的所有属性
        Field [] fields = c.getDeclaredFields();
        // 获取类对象的所有方法
        Method[] methods = c.getDeclaredMethods();
        try (
                PrintStream ps = new PrintStream(new FileOutputStream("D:\\git\\java-base\\ClassObject.txt", true));
                ){
            ps.println("==============类名：" + c.getName() + "==============");
            for (Field field : fields) {
                field.setAccessible(true);
                ps.println("属性名：" + field.getName() + "，属性类型：" + field.getType());
            }
            for (Method method : methods) {
                method.setAccessible(true);
                ps.println("方法名：" + method.getName() + "，方法返回类型：" + method.getReturnType());
            }
            ps.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
